package com.example.demoappsocket;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.example.demoappsocket.databinding.ActivityMianSocketClientBinding;

/**
 * @param methodParameters
 * @author Elliot
 * @description desc
 * @return methodReturnType
 * @time 2021-12-10: 10:13
 */
public class MianSocketClientActivity extends AppCompatActivity {

    private static String SOCKET_CLIENT = "socket_client";
    private ActivityMianSocketClientBinding mBinding;

    private SocketClient mSocketClient;

    Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case SocketClient.MSG_SOKECT_CONNECT_SUCCESS:
                    Toast.makeText(getContext(), "连接成功", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = ActivityMianSocketClientBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());
        mBinding.etServerIp.setText(PhoneUtils.getLocalIpAddress());
        mSocketClient = SocketClient.getInstance();
        mBinding.btnConnect.setOnClickListener(v -> {
            mSocketClient.initConnect(mBinding.etServerIp.getText().toString(), Integer.parseInt(mBinding.etServerPort.getText().toString()));
        });
        mSocketClient.setOnConnectListener(new SocketClient.ConnectListener() {
            @Override
            public void onConnectionSucceeded(String msg) {
                Log.d(SOCKET_CLIENT, "连接成功");
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onConnectionException(String msg) {
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onConnectionLoading(int i) {
                Log.d(SOCKET_CLIENT, "第 " + i + " 连接");
            }

            @Override
            public void onReceiveData(String data) {
                Log.d(SOCKET_CLIENT, "ReceiveData: " + data);
                mBinding.tvTxt.setText(data);
            }

            @Override
            public void onConnectError(String error) {
                Log.e(SOCKET_CLIENT, "onConnectError : " + error);
                Toast.makeText(getContext(), "重连失败，" + error, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onBrokenPipe(String msg) {
                Log.d(SOCKET_CLIENT, "onBrokenPipe: " + msg);
            }

            @Override
            public void onDisconnect() {
                Log.d(SOCKET_CLIENT, "断开连接");
            }

            @Override
            public void onInputShutDown(String msg) {
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onOutputShutDown(String msg) {
                Toast.makeText(getContext(), msg, Toast.LENGTH_SHORT).show();
            }
        });
        mBinding.btnSend.setOnClickListener(v -> {
            mSocketClient.sendData(mBinding.etMsg.getText().toString());
        });
        mBinding.btnDisconnect.setOnClickListener(v -> {
            mSocketClient.disconnect();
        });
    }

    private Context getContext() {
        return this;
    }
}